package com.huabo.audit.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.audit.enums.ProcessEnum;
import com.huabo.audit.oracle.dto.TblNbsjProjectDataDto;
import com.huabo.audit.oracle.entity.TblAttachment;
import com.huabo.audit.oracle.entity.TblNbsjProject;
import com.huabo.audit.oracle.entity.TblNbsjProjectDataEntity;
import com.huabo.audit.oracle.entity.TblProjectDataPreEntity;
import com.huabo.audit.oracle.mapper.TblAttachmentMapper;
import com.huabo.audit.oracle.mapper.TblNbsjProjectDataMapper;
import com.huabo.audit.oracle.vo.DataProVo;
import com.huabo.audit.service.ActivityPluginsService;
import com.huabo.audit.service.TblNbsjProjectDataProService;
import com.huabo.audit.service.TblNbsjProjectService;
import com.huabo.audit.util.PageInfo;
import com.huabo.audit.util.R;
@Service
public class TblNbsjProjectDataProServiceImpl extends ServiceImpl<TblNbsjProjectDataMapper, TblNbsjProjectDataEntity> implements TblNbsjProjectDataProService {

	@Autowired
    private TblNbsjProjectDataMapper tblNbsjProjectDataMapper;
    
    @Autowired
    private ActivityPluginsService activityPluginsService;
    
    @Resource
	private TblAttachmentMapper tblAttachmentMapper;
    
    @Resource
	private TblNbsjProjectService tblNbsjProjectService;
    
	
	@Override
	public void saveOrUpdate(TblProjectDataPreEntity dataPre) {
		// TODO Auto-generated method stub

	}

	@Override
	public void proDel(TblProjectDataPreEntity dataPre) {
		// TODO Auto-generated method stub

	}

	@Override
	public TblProjectDataPreEntity proDataById(BigDecimal id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addList(List<TblProjectDataPreEntity> tblProjectDataPres, String username, BigDecimal projectid,
			String projectname, String orgid) {
		// TODO Auto-generated method stub

	}

	@Override
	public Integer proDataById(String projectDataPreId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer selectProjectNumber(String projectDataPreId, String projectId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void updateTblProjectDataPre(TblProjectDataPreEntity dataPre) {
		// TODO Auto-generated method stub

	}
	
	
	
	//==
	@Override
	public JsonBean dataproPageList(String token, Integer pageNumber, Integer pageSize,DataProVo dataProVo,Integer orgid) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
    	if(pageNumber == null) {
    		pageNumber = 1;
    	}
    	if(pageSize==null) {
    		pageSize=15;
    	}
    	
//    	if(null == orgid) {
//    		orgid = loginStaff.getCurrentOrg().getOrgid().intValue();
//    	}
    	
    	Map<String,Object> resultMap = new HashMap<String,Object>(0);
    	
    	//==查询当前实施的项目！
		TblNbsjProject tnp = this.tblNbsjProjectService.getCurrenNbsjProjectByLoginStaff(loginStaff.getStaffid());
		if(tnp == null) {
			return ResponseFormat.retParam(0,30003,resultMap);
		}
		Integer projectId = tnp.getProjectId();
		if(null == projectId) {
			return ResponseFormat.retParam(0,30003,resultMap);
		}
    	
    	PageInfo<TblNbsjProjectDataEntity> pageInfo = new PageInfo<TblNbsjProjectDataEntity>();
//    	tblNbsjSheet.setAuditorgid(loginStaff.getCurrentOrg().getOrgid());
//    	pageInfo.setCondition(tblNbsjSheet);
    	pageInfo.setPageSize(pageSize);
    	pageInfo.setCurrentPage(pageNumber);
    	pageInfo.setTlist(this.tblNbsjProjectDataMapper.selectListByPageInfo(pageInfo,dataProVo,orgid,projectId));
    	pageInfo.setTotalRecord(this.tblNbsjProjectDataMapper.selectCountByPageInfo(pageInfo,dataProVo,orgid,projectId));
    	pageInfo.getTotalPage();
    	String identifier = activityPluginsService.getoNState(ProcessEnum.SJ_JHGL.name());
    	resultMap.put("identifier", identifier);
    	resultMap.put("pageInfo", pageInfo);
    	return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean dataproAdd(TblNbsjProjectDataEntity pd, String token,String attids) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
		Integer count = this.tblNbsjProjectDataMapper.selectPlanCodeByOrgid(pd);
		if(count > 0) {
			return ResponseFormat.retParam(0,202,null);
		}
		
		//==查询当前实施的项目！
		TblNbsjProject tnp = this.tblNbsjProjectService.getCurrenNbsjProjectByLoginStaff(loginStaff.getStaffid());
		if(tnp == null) {
			return ResponseFormat.retParam(0,30003,null);
		}
		Integer projectId = tnp.getProjectId();
		if(null == projectId) {
			return ResponseFormat.retParam(0,30003,null);
		}
		pd.setProjectId(projectId);
		
		BigDecimal orgid = loginStaff.getCurrentOrg().getOrgid();
    	
//		notice.setTblCreater(loginStaff);
		pd.setDataDate(new Date());
		pd.setOrgid(orgid+"");
		pd.setUsername(loginStaff.getRealname());
		
		//根据planId主键是否为空判断新增还是修改 ，主键为空新增、不为空修改；
		
		if(pd.getDataId() != null) {
			//修改；
			this.tblNbsjProjectDataMapper.updateEntity(pd);
			//==附件，先删除 再重新添加
			this.tblAttachmentMapper.deleteAttmentRelationDataPj(pd.getDataId());
			if (attids != null && !"".equals(attids)) {
				String[] ids = attids.split(",");
				for (String id : ids) {
					this.tblAttachmentMapper.insertAttmentRelationDataPj(id, pd.getDataId());
				}
			}
		}else {
			//新增；
			this.tblNbsjProjectDataMapper.insertEntity(pd);
			//==附件
			if (attids != null && !"".equals(attids)) {
				String[] ids = attids.split(",");
				for (String id : ids) {
					this.tblAttachmentMapper.insertAttmentRelationDataPj(id, pd.getDataId());
				}
			}
		}
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		resultMap.put("Doubtfulpoint",pd);
		return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean dataproDelete(Integer dataId, String token) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		TblNbsjProjectDataEntity plan = this.tblNbsjProjectDataMapper.selectById(dataId);
		
		if(plan == null) {
			return ResponseFormat.retParam(0,50001,null);
		}
		
//		if (plan.getOpinionstatus().equals(TblNbsjAuditplan.SPNO)) {
//			this.tblNbsjProjectDataMapper.deleteAuditPlanEntityById(planId);
//			return ResponseFormat.retParam(0,200,null);
//        } else {
//            return ResponseFormat.retParam(0,50001,null);
//        }
		this.tblNbsjProjectDataMapper.deleteById(dataId);
		return ResponseFormat.retParam(1,200,null);
	}

	@Override
	public JsonBean findDataProDetail(String token, Integer dataId) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		
		TblNbsjProjectDataEntity plan = this.tblNbsjProjectDataMapper.selectById(dataId);
		resultMap.put("Doubtfulpoint", plan);
		return ResponseFormat.retParam(1,200,resultMap);
	}
	
	@Override
	public R removeAttInfoByAttId(String token, String attId) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return R.fail("用户已失效！");
		}
        return this.deleteRealtionAttInfo(attId);
	}

	private R deleteRealtionAttInfo(String attId) throws Exception {
		boolean b = false;
        TblAttachment att = this.tblAttachmentMapper.selectEntityById(attId);
        this.tblNbsjProjectDataMapper.deleteFileInfoByAttId(att.getAttid().intValue());
        this.tblAttachmentMapper.deleteEntity(att.getAttid());
        
        return R.success();
	}

}
